package com.example.myapplication.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;
import com.example.myapplication.adapter.NewsAdapter;
import com.example.myapplication.adapter.Servlet;
import com.example.myapplication.adapter.Theme;
import com.example.myapplication.data.HomeBanner;
import com.example.myapplication.data.HomeServlet;
import com.example.myapplication.data.NewListData;
import com.example.myapplication.data.NewsSort;
import com.example.myapplication.data.ThemeData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends Fragment {
    private View view;
    private ViewPager banner;
    private List<HomeBanner.RowsDTO> bannerList;
    private Handler handler;
    private RecyclerView servlet;
    private List<HomeServlet.RowsDTO> servletList;
    private RecyclerView theme;
    private List<ThemeData.RowsDTO> themeList;
    private TabLayout news;
    private List<NewsSort.DataDTO> newsSortList;
    private List<NewListData.RowsDTO> newsList;
    private RecyclerView news_list;
    private SharedPreferences sharedPreferences;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_hoem, container, false);
        init();
        getBanner();
        getServlet();
        getTheme();
        getNewsList(14);
        getNewsSort();
        isToken();
        return view;
    }

    private void isToken() {
        if("".equals(OkHttpTool.TOKEN)){
            sharedPreferences = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
            String token = sharedPreferences.getString("token", "");
            OkHttpTool.TOKEN = token;
        }
    }


    private void getNewsList(int id) {
        String url = "prod-api/api/park/press/press/list?type="+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, NewListData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                NewListData newData = (NewListData) obj;
                newsList = newData.getRows();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        NewDataAdapter();
                    }
                });
            }

            @Override
            public void onFailure(String msg) {
            }
        });
    }
    private void NewDataAdapter() {
        NewsAdapter newsAdapter = new NewsAdapter(newsList,HomeFragment.this.getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        news_list.setLayoutManager(linearLayoutManager);
        news_list.setAdapter(newsAdapter);
    }
    private void getNewsSort() {

        String url = "prod-api/api/park/press/category/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, NewsSort.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                NewsSort newsSort = (NewsSort)obj;
                newsSortList = newsSort.getData();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        for (NewsSort.DataDTO dataDTO : newsSortList) {
                            TabLayout.Tab tab = news.newTab();
                            tab.setText(dataDTO.getName());
                            news.addTab(tab);
                        }
                        newsSortAdapter();
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void newsSortAdapter() {
        news.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String str = tab.getText().toString();
                int position = tab.getPosition();
                int id = newsSortList.get(position).getId();
                getNewsList(id);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void getTheme() {
        String url = "/prod-api/press/press/list?hot=Y";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ThemeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ThemeData themeData = (ThemeData)obj;
                themeList = themeData.getRows();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        themeAdapter();
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void themeAdapter() {
        Theme theme_1 = new Theme(themeList,HomeFragment.this.getContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(HomeFragment.this.getContext(),2);
        theme.setAdapter(theme_1);
        theme.setLayoutManager(gridLayoutManager);
    }

    private void getServlet() {
        String url = "prod-api/api/service/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, HomeServlet.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                HomeServlet homeServlet = (HomeServlet)obj;
                servletList = homeServlet.getRows();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        servletAdapter();
                    }
                });

            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void servletAdapter() {
        Servlet servlet_1 = new Servlet(servletList,HomeFragment.this.getContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(HomeFragment.this.getContext(),5);
        servlet.setAdapter(servlet_1);
        servlet.setLayoutManager(gridLayoutManager);
    }

    private void getBanner() {
        String url = "prod-api/api/rotation/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, HomeBanner.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                HomeBanner homeBanner = (HomeBanner)obj;
                bannerList = homeBanner.getRows();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<String> list = new ArrayList<>();
                        for (HomeBanner.RowsDTO rowsDTO : bannerList) {
                            list.add(rowsDTO.getAdvImg());
                        }
                        BannerTool bannerTool = new BannerTool(HomeFragment.this.getContext(),list,banner);
                        banner.setAdapter(bannerTool);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void init() {
        banner = view.findViewById(R.id.banner);
        handler = new Handler();
        servlet = view.findViewById(R.id.servlet_home);
        theme = view.findViewById(R.id.theme);
        news = view.findViewById(R.id.news);
        news_list = view.findViewById(R.id.news_list);

    }
}